package com.rover.system.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.Common;
import com.common.system.SpringContextHolder;
import com.common.system.SysConfigData;
import com.rover.system.dao.BaseDao;
import com.rover.system.entity.Permission;
import com.rover.system.entity.Role;
import com.rover.system.entity.User;
import com.utils.DateUtil;
import com.utils.Encrypt;

/**
 * 用户管理类.
 * 
 */
@Service
@Transactional
public class CommonService {
    @Autowired
    private BaseDao dao;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;
    
    @Autowired
    private  UserService userService;
    
    public Map<String, Object> getApplicationInfos() {
		SysConfigData manager = (SysConfigData) SpringContextHolder
				.getBean("sysConfigData");

		Map<String, Object> alls = new HashMap<String, Object>();

		alls.put("buildId", manager.getBuildId());
		alls.put("version", manager.getVersion());
		alls.put("productName", manager.getProductName());
		alls.put("vendor", manager.getVendor());
		alls.put("copyright", "Copyright &copy; " + manager.getYear() + " - "
				+ StringUtils.substring(DateUtil.format(new Date()), 0, 4)
				+ " &nbsp;" + manager.getVendor());
		alls.put("website", manager.getWebsite());

		Subject subject = SecurityUtils.getSubject();
		String loginName = (String) subject.getPrincipal();
		User user = userService.getUserByUserName(loginName);
		alls.put("userName",user.getName());

		return alls;
	}
    //初始
    @Transactional
    public void initData() {
    	 
        
        //设置系统管理员角色
        List<Role> roleList = new ArrayList<Role>();
        Role r = new Role();
        r.setName(Common.ADMIN_NAME);
        r.setDescription("系统管理员角色");
        roleList.add(r);
        this.dao.save(r);
        if (dao.countHql("select count(u.id) from User u",null) != 0l) {
            return;
        }
     
       
        //设置管理员用户
        User user = new User();
        user.setLoginName(Common.ADMIN_NAME);
        user.setName("管理员");
        //md5加密
        user.setPassword(Encrypt.md5(Common.ADMIN_PASSWORD));
        user.setRoleList(new HashSet(roleList));
        dao.save(user);

    }

    @Transactional
    public void initRolePermission() {
        Role role = roleService.getRoleByName(Common.ADMIN_NAME);
        List<Permission> permissionList = permissionService.getAllList();
        role.setPermissions(new HashSet(permissionList));
        dao.saveOrUpdate(role);
    }
}
